package ch.phovoz.tlog.listener;

import ch.phovoz.tlog.R;
import ch.phovoz.tlog.database.DatabaseManager;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

public class EditExersices extends ListActivity {
	
	private DatabaseManager dbmanager;
	private SQLiteDatabase tdatabase;
	
	
	public void onCreate(Bundle savedInstanceState) {
		
		DatabaseManager dbmanager= new DatabaseManager(this);
		tdatabase=dbmanager.getWritableDatabase();

		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.editexersice);
	     loadExersices();

}
	private void loadExersices(){
		Cursor ExersiceCursor = 
				tdatabase.rawQuery("SELECT * FROM "+DatabaseManager.TABLE_EXERSICENAME +";", null);
		startManagingCursor(ExersiceCursor);
		
		SimpleCursorAdapter exersiceAdapter=
				new SimpleCursorAdapter(this,
						android.R.layout.simple_list_item_1,
						ExersiceCursor,
						new String[]{"name"},
						new int[]{android.R.id.text1});
		
		setListAdapter(exersiceAdapter);
	}
	
	public void onAddButtonklick(View view){
		EditText editText = 
				(EditText) findViewById(R.id.editExersice_exersiceInputField);
		String text = editText.getText().toString();
		
		ContentValues values = new ContentValues(); 
		values.put("name", text);
		tdatabase.insert(DatabaseManager.TABLE_EXERSICENAME, null, values);
		editText.setText("");
		loadExersices();
		
		
	}
}